<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Teacher extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {

		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'teacher_model', 'teacher');
		$this->load->helper('text');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
		$this->load->dbutil();
	}
	
	// Hàm này thực thi việc tạo user cho các giáo viên đã có trong CSDL
	function do_action()
	{
		$this->teacher->creat_user();
		echo 'Done';
	}
	
	//Lấy tất cả danh sách Giáo viên
	public function index() 
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Giáo viên';
		$data['description'] = 'Danh sách Giáo viên';
		
		$this->load->library('basic_library');
		$this->load->library('pagination');
		$this->load->helper('url');
		$config = array();
		$config['base_url'] 	= base_url().'member/teacher/index/';
		$config['total_rows'] 	= $this->teacher->count_all_teacher();
		$config['per_page'] 	= 100;
		$config['uri_segment']	= 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu tiên';
		$config['last_link'] = 'Cuối cùng';
		$config['next_link'] = 'Tiếp theo';
		$config['prev_link'] = 'Lùi lại';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		$data['sotrang']	=	$this->uri->segment(4);
		$data['alldata'] 	= $this->teacher->get_all_teacher($config['per_page'], $page);
		$data['links'] 		= $this->pagination->create_links();
		
		$this->_template['page']	= 'users/teacher/teacher_list';
		$this->system_library->load($this->_template['page'], $data);
	
	}
	
	//Add teacher to giaovien table and create system account
	public function add_teacher()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm Giáo viên';
		$data['description'] = 'Thêm Giáo viên';
		$this->load->helper('form');
		
		$data['thongtinkhoa']	=	$this->teacher->get_all_khoa();
		
		// initialize upload photo
		$config['upload_path'] = './uploads/teachers';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '1000';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		//here is validation form
		$this->form_validation->set_rules('tengv', 'Tên GV', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|xss_clean|trim|valid_email');
		$this->form_validation->set_rules('diachi','Địa chỉ','required|max_length[255]');
		$this->form_validation->set_rules('didong','Di động','required|max_length[15]');
		$this->form_validation->set_rules('cmnd','CMND','required|numeric|callback_check_cmnd');
		
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			$button = $this->input->post('typebutton');
			if($_FILES['userfile']['name'] == '')
			{
				$data = null;
		
				$teacherid = $this->teacher->add_teacher($data);
				$this->session->set_flashdata('message', lang('successfully_update_profile'));
				if($button == 1)
				{
					redirect('member/teacher/edit_teacher/'.$teacherid, 'refresh');
				}
				else {
					redirect('member/teacher/', 'refresh');
				}
			}
			else
			{
				if(!$this->upload->do_upload())
				{
					$this->session->set_flashdata('message', $this->upload->display_errors());
					redirect($this->uri->uri_string());
				}
				else
				{
					$data = $this->upload->data();
					$teacherid = $this->teacher->add_teacher($data['file_name']);
					$this->session->set_flashdata('message', lang('successfully_created'));
					if($button == 1)
					{
						redirect('member/teacher/edit_teacher/'.$teacherid, 'refresh');
					}
					else {
						redirect('member/teacher/', 'refresh');
					}
				}
			}
		}
		
		$this->_template['page']	= 'users/teacher/teacher_add';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	//Update teacher info
	public function edit_teacher($teacherid)
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Sửa thông tin Giáo viên';
		$data['description'] = 'Sửa thông tin Giáo viên';
		$this->load->helper('form');
		
		$data['teacherinfo']	=	$this->teacher->get_teacher($teacherid);
		$data['list_congtac']	=	$this->teacher->get_teacher_cong_tac($teacherid);
		$data['thongtinkhoa']	=	$this->teacher->get_all_khoa();
		$user_id = $data['teacherinfo']->user_id;
		
		// initialize upload photo
		$config['upload_path'] = './uploads/teachers';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '1000';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		//here is validation form
		
		if($data['teacherinfo']->cmnd==$this->input->post('cmnd'))
		{
			$this->form_validation->set_rules('cmnd', 'CMND', 'required|max_length[50]|xss_clean');
		}
		else
		{
			$this->form_validation->set_rules('cmnd', 'CMND', 'required|max_length[50]|xss_clean|callback_check_cmnd');
		}
		$this->form_validation->set_rules('tengv', 'Tên GV', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|xss_clean|trim|valid_email');
		$this->form_validation->set_rules('diachi','Địa chỉ','required|max_length[255]');
		$this->form_validation->set_rules('didong','Di động','required|max_length[15]');
		
		
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			$button = $this->input->post('typebutton');
			if($_FILES['userfile']['name'] == '')
			{
				$data = null;
				
				$this->teacher->edit_teacher($teacherid, $user_id, $data);
				$this->session->set_flashdata('message', lang('successfully_update_profile'));
				if($button == 1)
				{
						redirect('member/teacher/edit_teacher/'.$teacherid, 'refresh');
				}
				else {
						redirect('member/teacher/', 'refresh');
				}
			}
			else
			{
				if(!$this->upload->do_upload())
				{
					$this->session->set_flashdata('message', $this->upload->display_errors());
					redirect($this->uri->uri_string());
				}
				else
				{
					$data = $this->upload->data();
					$this->teacher->edit_teacher($teacherid, $user_id, $data['file_name']);
					$this->session->set_flashdata('message', lang('successfully_created'));
					if($button == 1)
					{
						redirect('member/teacher/edit_teacher/'.$teacherid, 'refresh');
					}
					else {
						redirect('member/teacher/', 'refresh');
					}
				}
			}
		}
		
		$this->_template['page']	= 'users/teacher/teacher_edit';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function view($teacherid)
	{
		$data['ItemID']		 = 'User';
		$data['title']		 = "Xem thông tin Giáo viên";
		$data['description'] = "Xem thông tin Giáo viên";
		$data['info'] 	= $this->teacher->get_teacher($teacherid);			
		$data['congtac'] = $this->teacher->get_cong_tac($teacherid);
		$this->load->view('templates/default/layout/pages/users/teacher/view', $data);
	}
	
	// Thêm chứng từ
	public function them_cong_tac()
	{
	
		$start_time = $this->input->post('start_time');
		$end_time = $this->input->post('end_time');
		$noi_dung = $this->input->post('noi_dung');
		$cap_bac = $this->input->post('cap_bac');
		$time = $this->input->post('time');
		$cap_uy_dang = $this->input->post('cap_uy_dang');
		$teacher_id = $this->input->post('teacher_id');
		$return_id = $this->teacher->them_cong_tac($start_time,$end_time,$noi_dung,$cap_bac,$time,$cap_uy_dang,$teacher_id);
		
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	// Edit chứng từ
	public function save_cong_tac()
	{
		$id = $this->input->post('id');
		$start_time = $this->input->post('start_time');
		$end_time = $this->input->post('end_time');
		$noi_dung = $this->input->post('noi_dung');
		$cap_bac = $this->input->post('cap_bac');
		$time = $this->input->post('time');
		$cap_uy_dang = $this->input->post('cap_uy_dang');
	
		$return_id = $this->teacher->save_cong_tac($id,$start_time,$end_time,$noi_dung,$cap_bac, $time,$cap_uy_dang);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	
	public function ok()
	{
		$this->teacher->do_update_code();
		echo 'Thanh cong'; 
	}
	public function check_magv()
	{
		if($this->teacher->check_magv())
		{
			$this->form_validation->set_message('check_magv','MAGV đã tồn tại, vui lòng chọn lại');
			return false;
		}
		else
		{
	
			return true;
		}
	}
	public function check_cmnd()
	{
		if($this->teacher->check_cmnd())
		{
			$this->form_validation->set_message('check_cmnd','CMND đã tồn tại, vui lòng chọn lại');
			return false;
		}
		else
		{
	
			return true;
		}
	}
	
	public function add_lophoc_to_giaovien($gvid)
	{
		//$this->access->restrict('Member.Vattu.Add');
		$data['gvid'] = $gvid;
		$data['ItemID'] = 'User';
		$data['title'] = 'Biên chế Giáo viên dạy các lớp';
		$data['description'] = 'Biên chế Giáo viên dạy các lớp';
		$data['allclass'] = $this->teacher->get_all_class();
		$data['teacher_info'] = $this->teacher->get_teacher_info($gvid);
		$data['class_of_teacher'] = $this->teacher->get_class_of_teacher_by_id($gvid);
		$this->load->helper('form');
		//$data['dvt'] = $this->vattu->get_dvt_name();
		$userid = $this->user->id;
		$lang_working = load_language_working();
	
		$this->form_validation->set_rules('soluong', 'Số lượng', '');
		$this->form_validation->set_error_delimiters('', '<br />');
	
		// lấy danh sách các lớp đã được chọn và tách mảng
		$idlist = $this->input->post('listIds');
	
		$results = explode(",", $idlist);
		if($this->form_validation->run() == TRUE)
		{
			// Thực hiện add to
			$id = $this->teacher->add_lophoc_to_giaovien($results, $gvid);
			redirect('member/teacher', 'refresh');
		}
	
		$this->_template['page']	= 'users/teacher/add_lophoc_to_giaovien';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function search()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Tìm kiếm thông tin';
		$data['description'] = 'Tìm kiếm thông tin';
	
		$data['keyword'] = $this->input->post('keyword', true);
		$data['results'] = $this->teacher->search_teacher($data['keyword']);
	
		$this->_template['page']	= 'users/teacher/teacher_search';
		$this->system_library->load($this->_template['page'], $data);
	}
		
	public function teacher_exchange()
	{
		$id = $this->input->post('id');
		if($id !==null)
		{
			$data = $this->teacher->get_teacher($id);
			$status = 0;
			if($data -> status=='0')
			{
				$status = 1;
			}
			$this->teacher->teacher_exchange($id, $status);
			$row['result'] = 1;
			$row['status'] = $status;
			echo json_encode($row);
		}
		else
		{
			$row['result'] = 0;
			echo json_encode($row);
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/teacher.php */